<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\ActivityCat;
use App\Models\Activity;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ActivityCat\StoreRequest;
use App\Http\Requests\Admin\ActivityCat\UpdateRequest;
use Illuminate\Support\Facades\Input;
use App\Commands\OperationLogCommand;

use Gate;
use App\Exceptions\Admin\PermissionDeniedException;

class ActivityCatController extends Controller
{
    public function activity_cat_gate()
    {
        if (!Gate::allows('goods','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->activity_cat_gate();

        $activity_id = $request->get("id");

        if($activity_id){

            $activityCat = ActivityCat::where('activity_id',$activity_id)->orderBy('id','desc')->paginate(10);

            $is_activity = ActivityCat::where('activity_id',$activity_id)->first();

            if($is_activity){

                $activityCat = ActivityCat::orderBy('sort_order','desc')->orderBy('id','desc')->where('activity_id',$activity_id)->paginate(10);
             }

        }

        $activity = Activity::paginate(10);
        foreach($activity as $li){
            $activityarr[$li->id]=$li->name;

        }

        return view('admin.activity_cat.index',compact('activityCat','activity_id','activityarr'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request , ActivityCat $activityCat)
    {
        $this->activity_cat_gate();

        //dd($request->get("back_url"));
        $activity_id = $request->get("activity_id");
        return view('admin.activity_cat.create',compact('activityCat','activity_id'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreRequest $request , ActivityCat $activityCat)
    {
        $this->activity_cat_gate();

        //dd($request->get("back_url"));
        //dd($request->all());
        $param = $request->all();

        $param['activity_id']=$request->get("activity_id");
        //dd($param['activity_id']);
        $activityCat->create($param);
//        $this->dispatch(
//            new OperationLogCommand('添加了活动分类[activity_id:'.$param['activity_id'].',cat_name:'.$param['cat_name'].']')
//        );
        $this->dispatch(
            new OperationLogCommand('添加活动分类','ActivityCat','activity_id:'.$param['activity_id'])
        );

        return Redirect($request->get("back_url"));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($activity_id)
    {
        $this->activity_cat_gate();

        $activityCat = ActivityCat::where('activity_id', $activity_id)->get();
        $actCat = [];
        foreach ($activityCat as $key => $value) {
            $actCat[$value->id] = $value->cat_name;
        }//end foreach()

        return Response()->json($actCat);
    }//end func show()

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $this->activity_cat_gate();

        $activityCat = ActivityCat::find($id);
        return view('admin.activity_cat.edit',compact('activityCat'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRequest $request, $id)
    {
        $this->activity_cat_gate();

        //dd($request->get("back_url"));
        $activityCat = ActivityCat::find($id);

        $activityCat->update($request->all());
        $this->dispatch(
            new OperationLogCommand('修改活动分类','ActivityCat','id:'.$id)
        );

        return Redirect($request->get("back_url"));
        //return Redirect('admin/activity_cat');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request , $id)
    {
        $this->activity_cat_gate();

        //dd($request->get("url"));
        $return = ['status'=>0, 'message'=>'', 'data'=>''];
        $activityCat = ActivityCat::find($id);

        $activityCat->delete();
        $this->dispatch(
            new OperationLogCommand('删除活动分类','ActivityCat','id:'.$id)
        );
        return $return;
        //return Redirect($request->get("url"));
    }

    /**
     * 排序
     * @route admin.goods_attribute.sort
     * @return Response
     */
    public function sort(Request $request)
    {

        $this->activity_cat_gate();

        $url = $request->get('url');
        //dd($request->all());
        $activityCat = $request->get('sort_order');

        if (is_array($activityCat)&&count($activityCat)>0)
        {
            foreach($activityCat as $k=>$v)
            {

                $sort_order = ActivityCat::where('sort_order',$v)->get();

                ActivityCat::find($k)->update(['sort_order' => $v]);

            }
        }
//        $this->dispatch(
//            new OperationLogCommand('对活动分类进行了排序')
//        );

        return redirect($url);

    }
}
